﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuestionInvestig.Model;
using QuestionInvestig.MongoHelper;
using MongoDB.Driver;
using MongoDB.Bson;
namespace QuestionInvestig.Business
{
    /// <summary>
    /// 前台使用的业务
    /// </summary>
    public class QuestionForegroundService
    {
        private static readonly Mongo mongoHepler = new Mongo();

        /// <summary>
        /// 获取问卷信息的业务
        /// </summary>
        /// <param name="InvestigNo"></param>
        /// <param name="memId"></param>
        /// <returns></returns>
        public AnswerRecords GetInvestigInfo(string InvestigNo,string memId)
        {
            var ans = new AnswerRecords
            {
                AnswersTime = "",
                MemberId = memId,
                Phone = "",
                IdCardNumber = "",
                QuestionSettingId = 1,
                SerialId = "",
                UserName = "",
                AnswerQuestion = new List<AnswerQuestion>(),
            };
            if (string.IsNullOrWhiteSpace(InvestigNo) || string.IsNullOrWhiteSpace(memId))
            {
                return ans;
            }
            //查询问卷设置表
            var filter = Builders<BsonDocument>.Filter.Eq("QuestionSettingId", InvestigNo);
            var sort = Builders<BsonDocument>.Sort.Ascending("memno");
            var investDoc = mongoHepler.Find<BsonDocument>("QuestionConfiguration", filter, sort);
            if(investDoc==null || investDoc.Count == 0)
            {
                return ans;
            }
            //查询问卷题目表
            filter = Builders<BsonDocument>.Filter.Eq("QuestionSettingId", InvestigNo);
            sort = Builders<BsonDocument>.Sort.Ascending("QuestionSettingId");
            var questionDoc = mongoHepler.Find<BsonDocument>("QuestionList", filter, sort);
            if (questionDoc == null || questionDoc.Count == 0)
            {
                return null;
            }
            //查询答卷表
            var filterBulider=Builders< BsonDocument >.Filter;
            filter = filterBulider.Eq("QuestionSettingId", InvestigNo)& filterBulider.Eq("MemberId", memId);
            sort = Builders<BsonDocument>.Sort.Ascending("memno");
            var answerDoc = mongoHepler.Find<BsonDocument>("QuestionAnswers", filter, sort);

            //遍历问卷题目与答案，封装
            var question = ans.AnswerQuestion;
            foreach(var item in questionDoc)
            {
                var i=(Question)(MongoDB.Bson.Serialization.BsonSerializer.Deserialize(item,typeof(Question)));
                var answer = answerDoc.Any(t => t["OrderNum"].AsInt32 == i.OrderNum);
                var pair = new AnswerQuestion
                {
                    Question=i,
                    Answer=new List<Answer>(),
                };
                if (answer)
                {
                    var answers = answerDoc.Where(t => t["OrderNum"].AsInt32 == i.OrderNum).ToList();
                    foreach (var bson in answers)
                    {
                        pair.Answer.Add((Answer)(MongoDB.Bson.Serialization.BsonSerializer.Deserialize(bson, typeof(Answer))));
                    }
                }                
            } 
            return ans;
        }

        public AnswerRecords GetFakeInvestigInfo(string InvestigNo, string memId)
        {
            var ans = new AnswerRecords
            {
                AnswersTime = DateTime.Now.ToString(),
                MemberId = memId,
                Phone = "18862301026",
                IdCardNumber = "411423",
                QuestionSettingId = 1,
                SerialId = "sssss",
                UserName = "宋绪文",
                AnswerQuestion = new List<AnswerQuestion>(),
            };
            ans.AnswerQuestion.Add(new AnswerQuestion
            {
                Answer = new List<Answer> {
                    new Answer {
                        Content="测试答案1",
                        ItemNo="1",
                        OrderNum=1,
                        OtherValue=""  ,
                        QuestionId = "111",
                    }
                },
                Question = new Question
                {
                    QuestionId = "111",
                    QuestionTitle = "测试问题1",
                    Type = "2",
                    OrderNum = 1,
                    IsRequired = true,
                    QuestionItem = new List<QuestionItem> {
                        new QuestionItem
                        {
                            ItemNo="1",
                            Content="测试答案1",
                            IsHavingOther=false,
                            OrderNum=1,
                        },new QuestionItem
                        {
                            ItemNo="2",
                            Content="测试答案2",
                            IsHavingOther=false,
                            OrderNum=2,
                        },new QuestionItem
                        {
                            ItemNo="3",
                            Content="测试答案3",
                            IsHavingOther=false,
                            OrderNum=3,
                        }
                    }
                },                
            });
            ans.AnswerQuestion.Add(new AnswerQuestion
            {
                Answer = new List<Answer> {
                    new Answer {
                        Content="测试答案1",
                        ItemNo="1",
                        OrderNum=1,
                        OtherValue=""
                    }
                },
                Question = new Question
                {
                    QuestionId = "222",
                    QuestionTitle = "测试问题2",
                    Type = "2",
                    OrderNum = 2,
                    IsRequired = true,
                    QuestionItem = new List<QuestionItem> {
                        new QuestionItem
                        {
                            ItemNo="1",
                            Content="测试答案1",
                            IsHavingOther=false,
                            OrderNum=1,
                        },new QuestionItem
                        {
                            ItemNo="2",
                            Content="测试答案2",
                            IsHavingOther=false,
                            OrderNum=2,
                        },new QuestionItem
                        {
                            ItemNo="3",
                            Content="测试答案3",
                            IsHavingOther=false,
                            OrderNum=3,
                        }
                    }
                },
            });
            ans.AnswerQuestion.Add(new AnswerQuestion
            {
                Answer = new List<Answer> {
                    new Answer {
                        Content="这是文本内容",
                        ItemNo="1",
                        OrderNum=1,
                        OtherValue=""
                    }
                },
                Question = new Question
                {
                    QuestionId = "222",
                    QuestionTitle = "测试问题3",
                    Type = "3",
                    OrderNum = 3,
                    IsRequired = true,
                },
            });
            return ans;
        }

        public void Edit(string json)
        {
            var info = MongoHelpe.GetBsonDocument(json);
            mongoHepler.Insert<BsonDocument>(info, "QuestionAnswers");
            return;
        }
    }
}
